package org.redmaple.core.pkstrategy;

import java.util.Date;

import org.redmaple.core.util.DateUtils;

/**
 * 时间类型字符串主键生成策略
 * 
 * @author Xiangyun
 *
 */
public class TimeGeneratedStrategy extends AbstarctStringGeneratedStrategy {

	/**
	 * 系统默认的时间格式
	 */
	private static final String DEFAULT_SEPARATOR = "yyyyMMdd_HHmmss_SSS";

	/**
	 * 自定义的时间格式，默认为 yyyyMMdd_HHmmss_SSS
	 */
	private String timeFormatter = DEFAULT_SEPARATOR;

	/**
	 * 自定义的前缀
	 */
	private AbstarctStringGeneratedStrategy prefix;

	/**
	 * 自定义的后缀
	 */
	private AbstarctStringGeneratedStrategy suffix;

	public TimeGeneratedStrategy(String timeFormatter,
			AbstarctStringGeneratedStrategy prefix,
			AbstarctStringGeneratedStrategy suffix) {
		this.timeFormatter = timeFormatter;
		this.prefix = prefix;
		this.suffix = suffix;
	}

	public TimeGeneratedStrategy(AbstarctStringGeneratedStrategy prefix,
			AbstarctStringGeneratedStrategy suffix) {
		this.prefix = prefix;
		this.suffix = suffix;
	}

	public TimeGeneratedStrategy(String timeFormatter) {
		this.timeFormatter = timeFormatter;
		this.prefix = new StringGeneratedStrategy();
		this.suffix = new StringGeneratedStrategy();
	}

	public TimeGeneratedStrategy() {
		this.prefix = new StringGeneratedStrategy();
		this.suffix = new StringGeneratedStrategy();
	}

	public String getValue() {
		String dateValue = DateUtils.format(new Date(), timeFormatter);
		StringBuilder buffer = new StringBuilder();
		if (null != prefix) {
			buffer.append(prefix.getValue());
		}
		buffer.append(dateValue);
		if (null != suffix) {
			buffer.append(suffix.getValue());
		}
		return buffer.toString();
	}

}
